const express = require("express");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
const path = require("path");
const cors = require("cors");

const { setupRouter } = require("./routes/index");

const app = express();

logger.format("local-time", (tokens, req, res) => {
  // 获取本地标准时间（格式：YYYY-MM-DD HH:MM:SS.sss）
  const localTime = new Date()
    .toLocaleString("zh-CN", {
      year: "numeric",
      month: "2-digit",
      day: "2-digit",
      hour: "2-digit",
      minute: "2-digit",
      second: "2-digit",
      fractionalSecondDigits: 3,
      hour12: false,
    })
    .replace(",", ""); // 去除逗号分隔符
  return localTime;
});

app.use(logger(":method :url :status :response-time ms -- :local-time"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(cors());
app.use(express.static(path.join(__dirname, "public")));

// app.get("/admin", (req, res) => {
//   // 发送指定路径的 HTML 文件
//   const adminPagePath = path.join(__dirname, "public", "admin.html");
//   res.sendFile(adminPagePath, (err) => {
//     if (err) {
//       res.status(404).send("管理员页面不存在");
//     }
//   });
// });

setupRouter(app);

module.exports = app;
